﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using PcapDotNet.Base;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Arp;
using PcapDotNet.Packets.Ethernet;

namespace MolyMade.Mute
{
    public static class ArpPacket
    {
        public static Packet BuildArpPacket(
            string ethernetLayerSourceMacAddress,
            string ethernetLayerDestinationMacAddress,
            ArpOperation arpOperation,
            string arpLayerSourceMacAddress,
            string arpLayerDesinationMacAddress,
            string arpLayerSourceProtocolAddress,
            string arpLayerDesinationProtocolAddress)
        {
            string s1=ethernetLayerSourceMacAddress;
            string s2= ethernetLayerDestinationMacAddress;
            string s3= arpLayerSourceMacAddress;
            string s4 = arpLayerDesinationMacAddress;
            string s5=arpLayerSourceProtocolAddress;
            string s6=arpLayerDesinationProtocolAddress;
            EthernetLayer ethernetLayer = new EthernetLayer()
            {
                Source = new MacAddress(ethernetLayerSourceMacAddress),
                Destination = new MacAddress(ethernetLayerDestinationMacAddress),
                EtherType = EthernetType.None
            };
            ArpLayer arpLayer = new ArpLayer()
            {
                ProtocolType = EthernetType.IpV4,
                Operation = arpOperation,
                SenderHardwareAddress =  Mac.ToBytes(arpLayerSourceMacAddress).AsReadOnly(),
                SenderProtocolAddress = IPAddress.Parse(arpLayerSourceProtocolAddress).GetAddressBytes().AsReadOnly(),
                TargetHardwareAddress = Mac.ToBytes(arpLayerDesinationMacAddress).AsReadOnly(),
                TargetProtocolAddress = IPAddress.Parse(arpLayerDesinationProtocolAddress).GetAddressBytes().AsReadOnly()
            };
            PacketBuilder builder = new PacketBuilder(ethernetLayer,arpLayer);
            return builder.Build(DateTime.Now);
        }

        public static Packet BuildArpRequestPacket(
            string sourceMacAddress,
            string sourceIpAddress,
            string desinationIpAddress)
        {
            return BuildArpPacket(sourceMacAddress,
                Mac.Broadcast,
                ArpOperation.Request,
                sourceMacAddress,
                Mac.Null,
                sourceIpAddress,
                desinationIpAddress);
        }

        public static Packet BuildArpReplyPacket(
            string sourceMacAddress,
            string destinationMacAddress,
            string sourceProtocolAddress,
            string desinationProtocolAddress)
        {
            return BuildArpPacket(sourceMacAddress,
                destinationMacAddress,
                ArpOperation.Reply,
                sourceMacAddress,
                destinationMacAddress,
                sourceProtocolAddress,
                desinationProtocolAddress);
        }
    }
}
